package com.kit.data.kitopeso.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.viewpager.widget.ViewPager;

import com.kit.data.kitopeso.R;
import com.kit.data.kitopeso.adapter.GenderPagerAdapter;
import com.kit.data.kitopeso.analysis.libs.RateLib;
import com.kit.data.kitopeso.base.BaseActivity;
import com.kit.data.kitopeso.databinding.KitoScoreActivityBinding;
import com.kit.data.kitopeso.databinding.KitoScoreItemBinding;
import com.kit.data.kitopeso.net.PU;
import com.kit.data.kitopeso.obj.KitoScoreItem;
import com.kit.data.kitopeso.tools.LogUtil;
import com.kit.data.kitopeso.tools.ResUtils;
import com.kit.data.kitopeso.tools.ViewUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class KitoScoreActivity extends BaseActivity<KitoScoreActivityBinding> {


    int type;
    int score;
    List<KitoScoreItem> iconList;


    @Override
    protected KitoScoreActivityBinding initViewBinding(LayoutInflater inflater) {
        return KitoScoreActivityBinding.inflate(inflater);
    }

    @Override
    protected void initView() {
        addItemData();

        binding.tvCancel.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            finish();
        });
        binding.tvOk.setOnClickListener(view -> {
            ViewUtils.viewScale(view);
            PU.userBuriedPoint(PU.P69, "5");
            toNext();
        });
    }

    @Override
    protected void initData() {
        UP_TYPE1 = PU.P65;
        UP_TYPE2 = PU.P66;

        type = -1;

    }

    void addItemData() {

        iconList = new ArrayList<>();

        KitoScoreItem item1 = new KitoScoreItem();
        item1.setIcon1(R.mipmap.score_icon1);
        item1.setIcon2(R.mipmap.score_icon1_s);
        item1.setScore(1);

        KitoScoreItem item2 = new KitoScoreItem();
        item2.setIcon1(R.mipmap.score_icon2);
        item2.setIcon2(R.mipmap.score_icon2_s);
        item2.setScore(2);

        KitoScoreItem item3 = new KitoScoreItem();
        item3.setIcon1(R.mipmap.score_icon3);
        item3.setIcon2(R.mipmap.score_icon3_s);
        item3.setScore(3);

        KitoScoreItem item4 = new KitoScoreItem();
        item4.setIcon1(R.mipmap.score_icon4);
        item4.setIcon2(R.mipmap.score_icon4_s);
        item4.setScore(4);

        KitoScoreItem item5 = new KitoScoreItem();
        item5.setIcon1(R.mipmap.score_icon5);
        item5.setIcon2(R.mipmap.score_icon5_s);
        item5.setScore(5);

        iconList.add(item1);
        iconList.add(item2);
        iconList.add(item3);
        iconList.add(item4);
        iconList.add(item5);

        Collections.sort(iconList, new Comparator<KitoScoreItem>() {
            @Override
            public int compare(KitoScoreItem o1, KitoScoreItem o2) {
                if (o1.getScore() > o2.getScore()) {
                    return -1;
                }
                return 0;
            }
        });

        List<View> viewList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            KitoScoreItemBinding binding1 = KitoScoreItemBinding.inflate(inflater);
            KitoScoreItem item = iconList.get(i);
            binding1.tvIcon.setImageResource(item.getIcon1());
            binding1.tvTag.setVisibility(View.GONE);
            viewList.add(binding1.getRoot());
        }


        GenderPagerAdapter adater = new GenderPagerAdapter(viewList);
        binding.viewPager.setPageMargin(0);
        binding.viewPager.setAdapter(adater);
        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < viewList.size(); i++) {
                    View view = viewList.get(i);
                    ImageView tvTag = view.findViewById(R.id.tvTag);
                    ImageView tvIcon = view.findViewById(R.id.tvIcon);
                    KitoScoreItem item = iconList.get(i);
                    if (position == i) {
                        tvTag.setVisibility(View.VISIBLE);
                        tvIcon.setImageResource(item.getIcon2());
                        type = i;
                        score = item.getScore();
                    } else {
                        tvTag.setVisibility(View.GONE);
                        tvIcon.setImageResource(item.getIcon1());
                    }
                    if (i != position) {
                        view.setScaleX(0.8f);
                        view.setScaleY(0.8f);
                    } else {
                        view.setScaleX(1f);
                        view.setScaleY(1f);
                    }
                }


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    void toNext() {
        if (type < 0) {
            Toast.makeText(getActivity(), ResUtils.s(R.string.score_hit1), Toast.LENGTH_LONG).show();
            return;
        }
        LogUtil.e("me", "score->" + score);
        if (score >= 4) {
            RateLib.INSTANCE.launchFlow(this, aBoolean -> {
                LogUtil.e("me", "launchFlow->" + aBoolean);
                return null;
            });
        } else {
            Intent intent = new Intent(getActivity(), KitoFBSubmitActivity.class);
            intent.putExtra("score", score);
            startActivity(intent);
        }
        finish();
    }


}
